# -*- coding: utf-8 -*-
import tornado
import app.tools.SafeMonitorTools.api as api
import app.tools.SafeMonitorTools.m_position_layer_type as smType
from app.views import SmBaseHandler
mine_id = smType.default_MineId
import json
# 查询 人员分站的info信息
async def get_staff_info(self,node_id):
    rows = await api.query_staff_info(node_id,mine_id)
    readerInfo = dict()
    personList = []
    if len(rows) > 0:
        readerInfo['readerId'] = rows[0][0]
        readerInfo['count'] = rows[0][1]
        readerInfo['save_time'] = rows[0][2].ctime()
        readerInfo['alarm'] = rows[0][3]
        readerInfo['placement'] = rows[0][4]
        readerInfo['state'] = rows[0][5]
    for row in rows:
        person = dict()
        if row[6] is not None and row[7] is not None:
            person['pname'] = row[6]
            person['pdepartment'] = row[7]
            person['duty'] = row[8]
            person['state'] = row[9]
            person['rfid'] = row[10]
            person['intime'] = row[11] if row[11] is None else row[11].ctime()
            personList.append(person)
    readerInfo['p_list'] = personList
    self.finish({'data':readerInfo})
# 查询 摄像头的info信息
async def get_video_info(node_id):
    rows = await api.query_position(node_id)
    rowlen = len(rows)
    if rowlen > 0:
        # 提取row
        row = rows[0]
        respon = {}
        respon['mine_id'] = row[0]
        respon['node_id'] = row[1]
        respon['service_type'] = row[2]
        respon['x'] = row[3]
        respon['y'] = row[4]
        respon['layer'] = row[5]
        respon['rowguid'] = row[6]
        self.finish({'data':respon})
    else:
        self.finish({'data':{}})

# 查询 广播站的info信息
async def get_broadcast_info(self,node_id):
    rows = await api.query_broadcast_info(node_id,mine_id)
    rowlen = len(rows)
    if rowlen > 0:
        # 提取row
        row = rows[0]
        respon = {}
        respon['node_id'] = row[0]
        respon['node_type'] = row[1]
        respon['place'] = row[2]
        respon['layer'] = row[3]
        respon['lastupdatetime'] = row[4].ctime()
        respon['state'] = row[5]
        respon['alarm'] = row[6]
        self.finish({'data':respon})
    else:
        self.finish({'data':{}})

# 查询 gas的info信息
async def get_gas_info(node_id):
    rows = await api.query_position(node_id)
    rowlen = len(rows)
    if rowlen > 0:
        # 提取row
        row = rows[0]
        respon = {}
        respon['mine_id'] = row[0]
        respon['node_id'] = row[1]
        respon['service_type'] = row[2]
        respon['x'] = row[3]
        respon['y'] = row[4]
        respon['layer'] = row[5]
        respon['rowguid'] = row[6]
        self.finish({'data':respon})
    else:
        self.finish({'data':{}})

infoFuncDict = {
    smType.ServiceType["Staff"]: get_staff_info,
    smType.ServiceType["BroadCast"]:get_broadcast_info,
    smType.ServiceType["Video"]:get_video_info,
    smType.ServiceType["Gas"]:get_gas_info
}

class SmInfoHandler(SmBaseHandler):
    @tornado.gen.coroutine
    def get(self):
        node_id = self.get_query_argument('node_id')
        serviceType = self.get_query_argument('serviceType')
        yield infoFuncDict[serviceType](self,node_id)

